<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="出库日期">
            <el-date-picker
              v-model="daterangeChmCkrq"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              style="width: 215px"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="扫码出库日期">
            <el-date-picker
              v-model="daterangeChmCkrq1"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              style="width: 215px"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出货类型" prop="chmType">
            <el-select
              v-model="queryParams.chmType"
              clearable
              placeholder="请选择出货类型"
            >
              <el-option
                v-for="dict in allOutTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出库单号" prop="chdDjbh">
            <el-input
              v-model="queryParams.chdDjbh"
              placeholder="请输入出库单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单号" prop="chdSodh">
            <el-input
              v-model="queryParams.chdSodh"
              placeholder="请输入销售单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="chdWldm">
            <el-input
              v-model="queryParams.chdWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="chmKhdm">
            <el-select
              v-model="queryParams.chmKhdm"
              clearable
              placeholder="请选择客户"
            >
              <el-option
                v-for="dict in allCustomerOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="出库仓库" prop="ckDm">
            <el-select
              v-model="queryParams.ckDm"
              multiple
              placeholder="请选择出库仓库"
            >
              <el-option
                v-for="item in allStorageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(1)"
          size="small"
        >
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(2)"
          size="small"
        >
          物料统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(3)"
          size="small"
        >
          日期统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(4)"
          size="small"
        >
          公司出库
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(5)"
          size="small"
        >
          仓库统计
        </el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <u-table
          :pagination-show="true"
          :total="total"
          :page-size.sync="queryParams.pageSize"
          :current-page.sync="queryParams.pageNum"
          @handlePageSize="load"
          show-summary
          :total-option="[
            { label: '订单数量', unit: '' },
            { label: '出库数量', unit: '' },
          ]"
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load"
          :data="chdDetailList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="出货单号"
            align="center"
            width="135"
            prop="chdDjbh"
          />
          <el-table-column
            label="出货类型"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="chmType"
          >
            <template #default="scope">
              <dict-tag
                :options="allOutTypeOptions"
                :value="scope.row.chmType"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="客户"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="chmKhdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allCustomerOptions"
                :value="scope.row.chmKhdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="仓库"
            class-name="rkck"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="chdCkdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.chdCkdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="来源单号"
            width="135"
            align="center"
            prop="chdLydh"
          />
          <el-table-column
            label="销售单号"
            width="135"
            align="center"
            prop="chdSodh"
          />
          <el-table-column
            label="物料代码"
            width="135"
            align="center"
            prop="chdWldm"
          />
          <el-table-column
            label="品名规格"
            width="285"
            show-overflow-tooltip
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="订单数量"
            width="80"
            align="center"
            prop="chdDdsl"
          />
          <el-table-column
            label="出库数量"
            width="80"
            align="center"
            prop="chdCksl"
          />
          <!--          <el-table-column label="出货达成率" width="80" align="center" prop="chdChdcl" />-->
          <el-table-column
            label="存放位置"
            width="100"
            align="center"
            prop="chdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.chdCwdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            width="80"
            align="center"
            prop="chmDesc"
          />
          <el-table-column
            label="开单人员"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="chmJlry"
          />
          <el-table-column
            label="开单日期"
            :show-overflow-tooltip="true"
            align="center"
            width="150"
            prop="chmJlrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.chmJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="chmShry"
          />
          <el-table-column
            label="审核日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="chmShrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.chmShrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="出库人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="chdCkry"
          />
          <el-table-column
            label="出库日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="chdCkrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.chdCkrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="过账人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="chmGzry"
          />
          <el-table-column
            label="过账日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="chmGzrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.chdCkrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
        </u-table>
      </el-tab-pane>
      <el-tab-pane label="按物料统计" name="second">
        <u-table
          :pagination-show="true"
          :total="total"
          :page-size.sync="queryParams.pageSize"
          :current-page.sync="queryParams.pageNum"
          @handlePageSize="load"
          show-summary
          :total-option="[
            { label: '订单数量', unit: '' },
            { label: '出库数量', unit: '' },
          ]"
          border
          stripe
          :height="tableHChild"
          :data="chdWldmList"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column label="物料代码" align="center" prop="chdWldm" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="订单数量"
            width="80"
            align="center"
            prop="chdDdsl"
          />
          <el-table-column label="出库数量" align="center" prop="chdCksl" />
          <el-table-column
            label="出货达成率"
            width="80"
            align="center"
            prop="chdChdcl"
          />
        </u-table>
      </el-tab-pane>
      <el-tab-pane label="按日期统计" name="third">
        <u-table
          border
          stripe
          :height="tableHChild"
          :pagination-show="true"
          :total="total"
          :page-size.sync="queryParams.pageSize"
          :current-page.sync="queryParams.pageNum"
          @handlePageSize="load"
          show-summary
          :total-option="[
            { label: '订单数量', unit: '' },
            { label: '出库数量', unit: '' },
          ]"
          :data="chdDateList"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="过账日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="chmGzrq"
          />
          <el-table-column label="物料代码" align="center" prop="chdWldm" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          s
          <el-table-column
            label="订单数量"
            width="80"
            align="center"
            prop="chdDdsl"
          />
          <el-table-column label="出库数量" align="center" prop="chdCksl" />
          <el-table-column
            label="出货达成率"
            width="80"
            align="center"
            prop="chdChdcl"
          />
        </u-table>
      </el-tab-pane>
      <el-tab-pane label="公司出库单" name="fourth">
        <u-table
          border
          stripe
          :height="tableHChild"
          :pagination-show="true"
          :total="total"
          :page-size.sync="queryParams.pageSize"
          :current-page.sync="queryParams.pageNum"
          @handlePageSize="load"
          :data="chdCusList"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column label="出货单号" align="center" prop="chdDjbh" />
          <el-table-column
            label="客户"
            :show-overflow-tooltip="true"
            align="center"
            prop="chmKhdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allCustomerOptions"
                :value="scope.row.chmKhdm"
              />
            </template>
          </el-table-column>
          <el-table-column label="物料名称" align="center" prop="itmWlpm" />
          <el-table-column label="物料规格" align="center" prop="itmWlgg" />
          <el-table-column label="客户编号" align="center" prop="itmCustwldm" />
          <el-table-column label="物料代码" align="center" prop="chdWldm" />
          <el-table-column label="出库数量" align="center" prop="chdCksl" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column label="出库日期" align="center" prop="chdCkrq" />
        </u-table>
      </el-tab-pane>
      <el-tab-pane label="按仓库统计" name="fifth">
        <u-table
          border
          stripe
          :height="tableHChild"
          :pagination-show="true"
          :total="total"
          :page-size.sync="queryParams.pageSize"
          :current-page.sync="queryParams.pageNum"
          @handlePageSize="load"
          :data="chdCkdmList"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="仓库"
            class-name="rkck"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="chdCkdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.chdCkdm"
              />
            </template>
          </el-table-column>
          <el-table-column label="物料代码" align="center" prop="chdWldm" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column label="客户编号" align="center" prop="itmCustwldm" />
          <el-table-column label="出库数量" align="center" prop="chdCksl" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
        </u-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import mixin from "@/mixins/common";
import { listData, exportChmReport } from "@/api/output/proShipped";
import { getAllCustomerTwo } from "@/api/project/customer";

export default {
  name: "ChmAnalyse",
  mixins: [mixin.allStorageOptions, mixin.storageOptions, mixin.allCsOptions],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: 0,
      activeTab: "first",
      chdDetailList: [],
      chdDateList: [],
      chdCusList: [],
      chdCkdmList: [],
      chdWldmList: [],
      // 显示搜索条件
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      total3: 0,
      total4: 0,
      //所有客户代码字典
      allCustomerOptions: [],
      //所有出库类型字典
      allOutTypeOptions: [],
      //单位字典
      unitFlagOptions: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeChmCkrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      daterangeChmCkrq1: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        chdWldm: null,
        chdSodh: null,
        chdDjbh: null,
        chmKhdm: null,
        chmType: null,
        ckDm: null,
        index: 0,
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });

    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }
      this.allCustomerOptions = response.data;
    });
    //所有出货类型
    this.getAllDicts("sys_out_type").then((response) => {
      this.allOutTypeOptions = response.data;
      this.allOutTypeOptions.push({
        listClass: "default",
        dictValue: "CKTH",
        dictLabel: "退货入库",
      });
    });
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeChmCkrq && "" != this.daterangeChmCkrq) {
        this.queryParams.params["beginCkTime"] = this.moment(
          this.daterangeChmCkrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endCkTime"] = this.moment(
          this.daterangeChmCkrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      if (null != this.daterangeChmCkrq1 && "" != this.daterangeChmCkrq1) {
        this.queryParams.params["beginCkRrqTime"] = this.moment(
          this.daterangeChmCkrq1[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endCkRrqTime"] = this.moment(
          this.daterangeChmCkrq1[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listData(this.queryParams).then((response) => {
        this.chdDetailList = [];
        this.chdWldmList = [];
        this.chdDateList = [];
        this.chdCusList = [];
        this.chdCkdmList = [];
        //明细
        this.chdDetailList = this.chdDetailList.concat(
          response.data.detail.rows
        );
        this.total = response.data.detail.total;
        //物料
        this.chdWldmList = this.chdWldmList.concat(response.data.wldm.rows);
        this.total2 = response.data.wldm.total2;
        //日期
        this.chdDateList = this.chdDateList.concat(response.data.date.rows);
        this.total3 = response.data.date.total3;
        //公司出库单
        this.chdCusList = this.chdCusList.concat(response.data.cus.rows);
        this.total4 = response.data.cus.total4;
        //仓库
        this.chdCkdmList = this.chdCkdmList.concat(response.data.ckdm.rows);
        this.total5 = response.data.ckdm.total5;
        this.loading = false;
      });
    },
    load(res) {
      this.queryParams.pageNum = res.page;
      this.queryParams.pageSize = res.size;
      this.listed();
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total2 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },
    load3() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total3 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },
    load4() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total4 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },
    load5() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total5 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },
    listed() {
      listData(this.queryParams).then((response) => {
        //明细
        this.chdDetailList = response.data.detail.rows;
        this.total = response.data.detail.total;
        //物料
        this.chdWldmList = response.data.wldm.rows;
        this.total2 = response.data.wldm.total2;
        //日期
        this.chdDateList = response.data.date.rows;
        this.total3 = response.data.date.total3;
        //公司出库单
        this.chdCusList = response.data.cus.rows;
        this.total4 = response.data.cus.total4;
        //仓库
        this.chdCkdmList = response.data.ckdm.rows;
        this.total5 = response.data.ckdm.total5;
        this.loading = false;
      });
    },
    handleSearch() {
      this.openSearch = true;
      this.title = "查询来料进货统计分析";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    //导出Excel
    handleExcel(index) {
      this.queryParams.index = index;
      const queryParams = this.queryParams;
      exportChmReport(queryParams).then((response) => {
        this.download(response.msg);
        this.queryParams.index = 0;
      });
    },
  },
};
</script>
